<?php

namespace app\admin\model\chase;

use think\Model;
use traits\model\SoftDelete;

class Student extends Model
{

    use SoftDelete;
    protected $hidden = ['pivot'];
    
    

    // 表名
    protected $table = 'students';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'sex_text',
        'is_freeze_text',
        'age_text'
    ];
    

    
    public function getSexList()
    {
        return ['0' => __('Sex 0'), '1' => __('Sex 1'), '2' => __('Sex 2')];
    }

    public function getIsFreezeList()
    {
        return ['0' => __('Is_freeze 0'), '1' => __('Is_freeze 1')];
    }


    public function getSexTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sex']) ? $data['sex'] : '');
        $list = $this->getSexList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsFreezeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_freeze']) ? $data['is_freeze'] : '');
        $list = $this->getIsFreezeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getAgeTextAttr($value, $data)
    {
        $age = ceil((time() - strtotime($data['birthday'])) / (30 * 24 * 3600));
        $month = ($age % 12) ? $age % 12 . '个月' : '';
        return $data['birthday'] ? floor($age / 12) . '岁' . $month : '';
    }

    public function classes()
    {
        return $this->hasOne(Classes::class, 'id', 'class_id');
    }

    public static function students()
    {
        return self::where('is_freeze', 0)->select();
    }

    public static function getStudentsByClass($class_id)
    {
        return self::where('is_freeze', 0)->whereIn('class_id', $class_id)->select();
    }

    public static function getStudentIds($phone, $isSuperAdmin)
    {
        $class_id = Teacher::getClassess($phone, $isSuperAdmin);
        return self::where('is_freeze', 0)->whereIn('class_id', $class_id)->column('id');;
    }
}
